package cn.xyf.mzmy.server.mq.producer;

import cn.xyf.mzmy.server.mq.message.DemoMessage;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class DemoProducer {

    @Autowired
    private RedissonClient redissonClient;

    public void sendDemoMessage(DemoMessage demoMessage){
        // 获取主题（消息队列）
        RTopic topic = redissonClient.getTopic("demoQueue");
        // 发布消息
        topic.publish(demoMessage);
    }


    public void sendDelayMessage(DemoMessage demoMessage){
        // 获取延迟队列
        RBlockingDeque<DemoMessage> blockingDeque = redissonClient.getBlockingDeque("delayQueue");
        RDelayedQueue<DemoMessage> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        // 延迟发送消息
        delayedQueue.offer(demoMessage,5, TimeUnit.SECONDS);
        System.out.println("消息发送成功");
    }


}
